//
//  RankListAvatarCell.swift
//  MM2
//
//  Created by 李成明 on 2022/1/7.
//

import UIKit
import JXPhotoBrowser

class RankListImageCell: UITableViewCell {
    
    static let cellHeight: CGFloat = 120
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        layer.masksToBounds = true
        layer.cornerRadius = 10
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var model: RankListModel? {
        didSet {
            updateUI()
        }
    }
    
    private lazy var bgView: UIImageView = {
        let view = UIImageView()
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 10
        view.alpha = 0.3
        return view
    }()
    
    private lazy var rankNumIcon: UIImageView = {
        let view = UIImageView()
        return view
    }()
    
    private lazy var rankNumLabel: UILabel = {
        let view = UILabel()
        view.font = UIFont(name: faceValueFontStr, size: 24)
        view.textAlignment = .center
        return view
    }()
    
    private lazy var faceImage: UIImageView = {
        let view = UIImageView()
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 8
        view.contentMode = .scaleAspectFit
        view.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(imageClick(sender:)))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    private lazy var faceValueLabel: UILabel = {
        let view = UILabel()
        view.font = UIFont(name: faceValueFontStr, size: 30)
        view.textColor = faceValueColor
        return view
    }()
    
    private lazy var nameLabel: UILabel = {
        let view = UILabel()
        view.font = .systemFont(ofSize: 16)
        view.numberOfLines = 0
        view.textAlignment = .center
        return view
    }()
}

extension RankListImageCell {
    private func setupUI() {
        contentView.addsubViews([bgView, nameLabel, rankNumIcon, rankNumLabel, faceImage, faceValueLabel])
        
        bgView.snp.makeConstraints { make in
            make.left.top.right.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-8)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(contentView).offset(16)
            make.centerY.equalTo(contentView)
            make.width.equalTo(50)
        }
        
        rankNumIcon.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(16)
            make.centerY.equalTo(contentView)
            make.size.equalTo(CGSize(width: 32, height: 32))
        }
        
        rankNumLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(16)
            make.centerY.equalTo(contentView)
            make.size.equalTo(CGSize(width: 32, height: 32))
        }
        
        faceImage.snp.makeConstraints { make in
            make.left.equalTo(rankNumIcon.snp.right).offset(16)
            make.centerY.equalTo(contentView)
            make.size.equalTo(CGSize(width: 80, height: 80))
        }
        
        faceValueLabel.snp.makeConstraints { make in
            make.left.equalTo(faceImage.snp.right).offset(20)
            make.top.right.bottom.equalTo(contentView)
        }
    }
    
    private func updateUI() {
        guard let model = model else { return }
        nameLabel.text = model.userName
        
        rankNumIcon.isHidden = model.rankNum > 3
        rankNumLabel.isHidden = !(model.rankNum > 3)
        if model.rankNum == 1 {
            rankNumIcon.image = UIImage(named: "rank_first_icon")
        } else if model.rankNum == 2 {
            rankNumIcon.image = UIImage(named: "rank_second_icon")
        } else if model.rankNum == 3 {
            rankNumIcon.image = UIImage(named: "rank_third_icon")
        } else {
            rankNumLabel.text = "\(model.rankNum)"
        }
        
        bgView.image = (model.rankNum%2 == 0) ? UIImage(named: "rank_red_bgimg") : UIImage(named: "rank_green_bgimg")
        
        faceImage.setImageWithUrlStr(model.imgUrl)
        
        let attrStr = NSMutableAttributedString.init(string: String(format: "%.2f", model.faceValue),attributes: [.font: UIFont.systemFont(ofSize: 16)])
        attrStr.addAttributes([.font: UIFont(name: "Verdana-Italic", size: 70)!], range: NSRange(location: 0, length: model.faceValue > 10 ? 2 : 1))
        faceValueLabel.attributedText = attrStr
    }
    
    @objc private func imageClick(sender: UITapGestureRecognizer) {
        
        let browser = JXPhotoBrowser()
        browser.numberOfItems = {
            return 1
        }
        browser.reloadCellAtIndex = { context in
            let browserCell = context.cell as? JXPhotoBrowserImageCell
            browserCell?.imageView.setImageWithUrlStr(self.model?.imgUrl)
        }
        
        browser.transitionAnimator = JXPhotoBrowserSmoothZoomAnimator(transitionViewAndFrame: { (index, destinationView) -> JXPhotoBrowserSmoothZoomAnimator.TransitionViewAndFrame? in
            let transitionView = UIImageView()
            transitionView.setImageWithUrlStr(self.model?.imgUrl)
            transitionView.contentMode = .scaleAspectFit
            transitionView.clipsToBounds = true
        
            let thumbnailFrame = self.faceImage.convert(self.faceImage.bounds, to: destinationView)
            return (transitionView, thumbnailFrame)
        })
        
        browser.show()
    }
}
